var db = require('../libs/db');
var assert = require('assert');

var table = 'student';


var addStudent = function(){

	var data = {
			'name' : 'ranran11',
			'age' : '2',
			'email' : 'ranran@163.com',
			'addess' : '襄阳-宜城',
	    }

	var insertDocument = function(db,callback) {
	    db.collection(table).insertOne( data, function(err, result) {
	    assert.equal(err, null);
	    console.log("Inserted a document into the restaurants collection.");
	    callback(result);
	  });
	}

	db.exec(insertDocument);
}

// addStudent();
var selectStudent = function(cal){
	var data = {'name' : 'yinpeng'};
	var func = function(db,callback){
		var curor = db.collection('student').find(data);
		curor.toArray(function(err,doc){
			assert.equal(err, null);
			callback(doc);

			cal(doc);			//控制器回调方法
		});
	}
	db.exec(func);
}

/*
selectStudent(function(doc){
	console.log(doc);
});
*/

exports.selectStudent = selectStudent;				//查找学生
exports.addStudent = addStudent;					//添加学生